<?php
namespace app\house\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\house\model\AdminArea as area_model;
class AdminArea extends AdminRest
{

    protected $model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new area_model();

    }

    /**
     * 房产区域列表
     */
    public function areaList(){
        $data = $this->model->areaList(['uniacid'=>$this->_uniacid]);
        return $this->success($data);
    }
    /**
     *
     * 添加房产区域
     */
    public function areaAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $input['pid']     = $this->changeData($input['pid']);
        $input['level']   = $this->model->getLevel($input['pid']);
        $res = $this->model->areaAdd($input);
        return $this->success($res);
    }
    /**
     *
     * 编辑房产区域
     */
    public function areaUpdate(){
        $input = $this->_input;
        $data  = $this->model->areaInfo(['id'=>$input['id']]);
        if(count($input)>1){
            $input['pid'] = $this->changeData($input['pid']);
            $level        = $this->model->areaInfo(['id'=>$input['pid']]);
            if(!empty($level)&&$data['id']==$level['id']){
                return $this->error('上级不能是自己');
            }
            if(!empty($level)&&$this->model->getLevel($data['pid'])<$this->model->getLevel($level['pid'])){
                return $this->error('所选上级等级不能小于自身等级');
            }
            $input['level'] = $this->model->getLevel($input['pid']);
            $data = $this->model->areaUpdate(['id'=>$input['id']],$input);
        }else{
            $data['pid'] = $this->getLevel($data['pid']);
        }
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-16 14:38
     * @return void
     * descption:转换数据 上级
     */
    public function changeData($data){
        if(is_array($data)){
            $data = array_pop($data);
        }
        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-09-16 14:57
     * @return void
     * descption:回显转上级格式
     *
     */
    public function getLevel($id){
        static $data = array();
        $data[]= $id;
        $dis[] = ['id','=',$id];
        $dis[] = ['status','>',-1];
        $level = $this->model->where($dis)->value('pid');
        if(!empty($level)&&!in_array($level,$data)){
            array_unshift($data,$level);
        }
        return $data;
    }
    /**
     *
     * 获取可以前两级的分类
     */
    public function levelArea(){
        $dis[]=['uniacid','=',$this->_uniacid];
        $data = $this->model->areaListAdmin($dis,2);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-27 19:03
     * @return \think\Response
     * descption:删除房产区域
     */
    public function areaDel(){
        $input = $this->_input;
        $res   = $this->model->areaDel(['id'=>$input['id']]);
        return $this->success($res);
    }






}
